package controllers;

import entities.ArsFAMILIA;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.zkoss.zul.Button;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;
import render.FamiliasRenderer;

public class FamiliasViewController extends GenericForwardComposer {
    Window winFamilias;
    
    Textbox txtFamilia;
    
    Listbox familiaListBox;
    Button cleanButton;
    Textbox filtroCodigo;
    Textbox filtroDescripcion;
    
    ArsFAMILIA familiaToSet;
    
    String familiaSelected;
    
    @Override
    public void doAfterCompose (Component comp) throws Exception {
        super.doAfterCompose(comp);
        
        // set models and render to listbox after composed
        familiaListBox.setModel(getFamiliasModel());
        familiaListBox.setItemRenderer(new FamiliasRenderer());
    }

    public ListModel getFamiliasModel () {
        List familias;
        
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from ArsFAMILIA as a");
        familias = q.getResultList();
        return new ListModelList(familias);
    }
    
    public ListModel getFamiliasByCodigo () {
        List familias;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from ArsFAMILIA as a WHERE a.codigo LIKE :codigo AND a.descripcion LIKE :descripcion ORDER By a.codigo ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        familias = q.getResultList();
        return new ListModelList(familias);
    }
    
    public ListModel getFamiliasByDescripcion () {
        List familias;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from ArsFAMILIA as a WHERE a.codigo LIKE :codigo AND a.descripcion LIKE :descripcion ORDER By a.descripcion ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        familias = q.getResultList();
        return new ListModelList(familias);
    }
    
    public void onChange$filtroCodigo() {
        familiaListBox.setModel(getFamiliasByCodigo());
        familiaListBox.setItemRenderer(new FamiliasRenderer());
    }
    
    public void onChange$filtroDescripcion() {
        familiaListBox.setModel(getFamiliasByDescripcion());
        familiaListBox.setItemRenderer(new FamiliasRenderer());
    }
    
    public void onClick$cleanButton() {
        filtroCodigo.setValue("");
        filtroDescripcion.setValue("");
        familiaListBox.setModel(getFamiliasModel());
        familiaListBox.setItemRenderer(new FamiliasRenderer());
    }
}